package com.xueshenggaunli;

import java.util.ArrayList;
import java.util.Scanner;

public class JavaMain {
    public static void main(String[] args) {
        ArrayList<student> array = new ArrayList<student>();
        while (true){
            System.out.println("----------欢迎来到学生管理系统----------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看所有学生");
            System.out.println("5 查找学生");
            System.out.println("6 退出");
            System.out.println("请输入你的选择： ");


            Scanner sc = new Scanner(System.in);
            String line=sc.nextLine();

            switch (line){
                case "1":
                    addStudent(array);
                    break;
                case "2":
                    deleteStudent(array);
                    break;
                case "3":
                    updateStudent(array);
                    break;
                case "4":
                    findAllStudent(array);
                    break;
                case "5":
                    findStudent(array);
                    break;
                case "6":
                    System.out.println("谢谢使用");
                    System.exit(0);
                    break;

            }
        }
    }
    public static void addStudent(ArrayList<student> array){
        Scanner sc = new Scanner(System.in);
        String sid;
        while (true){
            System.out.println("请输入学号：");
            sid = sc.nextLine();
            boolean flag = isUsed(array,sid);
            if (flag){
                System.out.println("输入的学号被占用，重新输入");
            }else {
                break;
            }
        }
        System.out.println("输入学生姓名：");
        String name = sc.nextLine();
        System.out.println("输入学生年龄：");
        String age = sc.nextLine();
        System.out.println("输入学生居住地");
        String address = sc.nextLine();

        student s = new student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);

        array.add(s);
        System.out.println("添加成功");
    }
    public static boolean isUsed(ArrayList<student> array,String sid){
        boolean flag = false;
        for (int i = 0;i<array.size();i++){
            student s = array.get(i);
            if (s.getSid().equals(sid)){
                flag = true;
                break;
            }
        }
        return flag;
    }
    public static  void findStudent(ArrayList<student> array){
        if(array.size()==0){
            System.out.println("无信息，请先添加再查询");
            return;
        }
        Scanner sc = new Scanner(System.in);
        String name;
        while (true){
            System.out.println("输入学生姓名：");
            name = sc.nextLine();
            for (int i = 0;i< array.size();i++){
                student s = array.get(i);
                if (s.getName().equals(name)){
                    System.out.println("学号："+s.getSid()+"\t"+"名字："+s.getName()+"\t"+"年龄"+s.getAge()+"\t"+"居住地"+s.getAddress());
                }
            }
            break;
        }
    }
    public static void deleteStudent(ArrayList<student>array){
        if(array.size()==0){
            System.out.println("无信息，请先添加再查询");
            return;
        }
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("输入你要删除的学生的学号：");
            String sid = sc.nextLine();
            int index = -1;
            for (int i = 0; i < array.size(); i++) {
                student s = array.get(i);
                if (s.getSid().equals(sid)) {
                    index = i;
                    break;
                }
            }
            if (index == -1) {
                System.out.println("该信息不存在，重新输入");
            } else {
                array.remove(index);
                System.out.println("删除学生成功");
                break;
            }
        }
    }
    public static void updateStudent(ArrayList<student>array){
        if(array.size()==0){
            System.out.println("无信息，请先添加再查询");
            return;
        }
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入要修改信息的学生的学号");
            String sid = sc.nextLine();
            int index = -1;
            for (int i = 0; i < array.size(); i++) {
                student s = array.get(i);
                if (s.getSid().equals(sid)) {
                    index = i;
                    break;
                }
            }
            if (index == -1) {
                System.out.println("该信息不存在，重新输入");
            } else {
                System.out.println("请输入学生新姓名：");
                String name = sc.nextLine();
                System.out.println("请输入学生新年龄：");
                String age = sc.nextLine();
                System.out.println("请输入学生新居住地：");
                String address = sc.nextLine();
                student s = new student();
                s.setSid(sid);
                s.setName(name);
                s.setAge(age);
                s.setAddress(address);
                array.set(index, s);
                System.out.println("修改学生信息成功");
                break;
            }
        }
    }
    public static void findAllStudent(ArrayList<student>array){
        if(array.size()==0){
            System.out.println("无信息，请先添加再查询");
            return;
        }

        for (int i=0;i< array.size();i++){
            student s = array.get(i);
            System.out.println("学号："+s.getSid()+"\t"+"名字："+s.getName()+"\t"+"年龄"+s.getAge()+"\t"+"居住地"+s.getAddress());
        }
    }
}

























